package cn.edu.sgu.www.mhxysy.service.qiling.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.qiling.QilingTaozhuangEffectDTO;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingTaozhuangEffect;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingTaozhuangEffectMapper;
import cn.edu.sgu.www.mhxysy.pager.qiling.QilingTaozhuangEffectPager;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingTaozhuangEffectService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class QilingTaozhuangEffectServiceImpl implements QilingTaozhuangEffectService {

    private final QilingTaozhuangEffectMapper qilingTaozhuangEffectMapper;

    @Autowired
    public QilingTaozhuangEffectServiceImpl(QilingTaozhuangEffectMapper qilingTaozhuangEffectMapper) {
        this.qilingTaozhuangEffectMapper = qilingTaozhuangEffectMapper;
    }

    @Override
    public void insert(QilingTaozhuangEffectDTO qilingTaozhuangEffectDTO) {
        QilingTaozhuangEffect qilingTaozhuangEffect = toEntity(qilingTaozhuangEffectDTO);

        qilingTaozhuangEffectDTO.setId(null);

        qilingTaozhuangEffectMapper.insert(qilingTaozhuangEffect);
    }

    @Override
    public void updateById(QilingTaozhuangEffectDTO qilingTaozhuangEffectDTO) {
        ObjectUtils.requireNonNull(qilingTaozhuangEffectDTO.getId(), CustomMessages.ID_NOT_NULL);

        QilingTaozhuangEffect qilingTaozhuangEffect = toEntity(qilingTaozhuangEffectDTO);

        qilingTaozhuangEffectMapper.updateById(qilingTaozhuangEffect);
    }

    @Override
    public Page<QilingTaozhuangEffect> selectByPage(QilingTaozhuangEffectPager pager) {
        QueryWrapper<QilingTaozhuangEffect> queryWrapper = new QueryWrapper<>();
        Page<QilingTaozhuangEffect> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getTaozhuangId() != null,
                "taozhuang_id", pager.getTaozhuangId()
        );

        return qilingTaozhuangEffectMapper.selectPage(page, queryWrapper);
    }

    private QilingTaozhuangEffect toEntity(QilingTaozhuangEffectDTO qilingTaozhuangEffectDTO) {
        QilingTaozhuangEffect qilingTaozhuangEffect = new QilingTaozhuangEffect();

        qilingTaozhuangEffect.setId(qilingTaozhuangEffectDTO.getId());
        qilingTaozhuangEffect.setPropertyValue(qilingTaozhuangEffectDTO.getPropertyValue());
        qilingTaozhuangEffect.setCount(qilingTaozhuangEffectDTO.getCount());
        qilingTaozhuangEffect.setPropertyId(qilingTaozhuangEffectDTO.getPropertyId());
        qilingTaozhuangEffect.setTaozhuangId(qilingTaozhuangEffectDTO.getTaozhuangId());

        return qilingTaozhuangEffect;
    }

}